<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of profile
 *
 * @author miyzan
 */
class profile extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('profile_model', 'prof');
        $this->load->library('../controllers/global_reference');
    }

    public function formload() {
        $profile = $this->global_reference->get_profile();
        $namasekolah = $profile[0]->nama_sekolah;
        $alamat = $profile[0]->alamat_sekolah;
        $path_url = parse_url($profile[0]->url_logo, PHP_URL_PATH);
        $idbpr = $profile[0]->id_nasabah_bpr;
        $arr = explode('/', $path_url);
        $urlllogo = '';
        for ($i = 0; $i < count($arr); $i++) {
            if ($i > 1) {
                $urlllogo.=$arr[$i];
                if ($i < count($arr) - 1) {
                    $urlllogo.='/';
                }
            }
        }
        $profile[0]->url_logo = $arr[count($arr) - 1];
//        $data = array('nama_sekolah' => date('Y-m-d'), 'id_th_ajaran' => $thajaran, 'semester' => $semester);
        echo '{"success":true,data:' . json_encode($profile[0]) . '}';
    }

    public function deleteImage() {
        $dir = "assets/images/logo/";
//        $images = array();
        $d = dir($dir);
        while ($name = $d->read()) {
            if (!preg_match('/\.(jpg|gif|png)$/', $name))
                continue;
            unlink($dir . $name);
        }
    }

    public function getImage() {
        $profile = $this->global_reference->get_profile();
        $path_url = parse_url($profile[0]->url_logo, PHP_URL_PATH);
        $arr = explode('/', $path_url);
        $urlllogo = '';
        for ($i = 0; $i < count($arr); $i++) {
            if ($i > 1) {
                $urlllogo.=$arr[$i];
                if ($i < count($arr) - 1) {
                    $urlllogo.='/';
                }
            }
        }
        $logoname = $arr[count($arr) - 1];
        $dir = "assets/images/logo/";
        $images = array();
        $d = dir($dir);
        while ($name = $d->read()) {
            if (!preg_match('/\.(jpg|gif|png)$/', $name))
                continue;
            if ($name == $logoname) {
//                $size = filesize($dir.$name);
//            $lastmod = filemtime($dir.$name)*1000;
                $images[] = array('caption' => $name, 'src' => base_url() . $dir . $name);
            }
        }

//        while($name = $d->read()){
//            if(!preg_match('/\.(jpg|gif|png)$/', $name)) continue;
//            if($name==$logoname){
//                $size = filesize($dir.$name);
//            $lastmod = filemtime($dir.$name)*1000;
//            $images[] = array('name'=>$name, 'size'=>$size,
//                                'lastmod'=>$lastmod, 'url'=>  base_url().$dir.$name);
//            }
//            
//        }
        $d->close();
        $o = array('data' => $images);
        $data = json_encode($images);
        $total = count($images);
        echo "{success:true,record:$total,data:$data }";
    }
    
    public function update_rows() {
        $opt = isset($_POST['cmd']) ? $this->db->escape_str($this->input->post('cmd', TRUE)) : '';
        $ns=isset($_POST['nama_sekolah']) ? $this->db->escape_str($this->input->post('nama_sekolah', TRUE)) : '';
        $al=isset($_POST['alamat_sekolah']) ? $this->db->escape_str($this->input->post('alamat_sekolah', TRUE)) : '';
        $idn = isset($_POST['id_nasabah_bpr']) ? json_decode($this->input->post('id_nasabah_bpr', TRUE)): array(); 
//        $result='';
        $logo='';
        $spname='sp_profile';
        $param=array($opt,$ns,$al,$logo,$idn);
        $result = $this->prof->SP_execData($spname, $param, true);
        echo $result;   
    }
    
    public function update_logo($logo='') {
        $opt = 'logo';
//        $dir = "assets/images/logo/";
//        $result='';
        $spname='sp_profile';
        $param=array($opt,'','',$logo,'');
        $result = $this->prof->SP_execData($spname, $param, true);
        return $result;   
    }
    
    public function upload_file() {
        $dir = "assets/images/logo/";
        $allowedExts = array(
            "gif",
            "jpeg",
            "jpg",
            "png"
        );
        $temp = explode(".", $_FILES["photo-path"]["name"]);
        $extension = end($temp);

        $infoString = "";
        $file = $_FILES['photo-path'];
        $fileName = $file['name'];
        $fileSize = $file['size'];

        if ((($_FILES["photo-path"]["type"] == "image/gif") || ($_FILES["photo-path"]["type"] == "image/jpeg") || ($_FILES["photo-path"]["type"] == "image/jpg") || ($_FILES["photo-path"]["type"] == "image/pjpeg") || ($_FILES["photo-path"]["type"] == "image/x-png") || ($_FILES["photo-path"]["type"] == "image/png")) && ($_FILES["photo-path"]["size"] < 9999999)
//&& ($_FILES["photo-path"]["size"] < 20000) 
                && in_array($extension, $allowedExts)) {
            if ($_FILES["photo-path"]["error"] > 0) {
                die("{'success': false, 'msg': '" . $_FILES["photo-path"]["error"] . "'}");
            } else {
                $infoString .= " Upload: " . $_FILES["photo-path"]["name"] . "<br>";
                $infoString .= " Type: " . $_FILES["photo-path"]["type"] . "<br>";
                $infoString .= " Size: " . ($_FILES["photo-path"]["size"] / 1024) . " kB<br>";
                $infoString .= " Temp file: " . $_FILES["photo-path"]["tmp_name"] . "<br>";

                if (file_exists($dir . $_FILES["photo-path"]["name"])) {
                    die("{'success': false, 'msg': '" . $_FILES["photo-path"]["name"] . " already exists.'}");
                } else {
                    $this->deleteImage();
                    move_uploaded_file($_FILES["photo-path"]["tmp_name"], $dir . $_FILES["photo-path"]["name"]);
                    $infoString .= " Stored in: " . $dir . $_FILES["photo-path"]["name"];
                    $result=$this->update_logo(base_url().$dir.$fileName);
                    $r=json_decode($result);
                    echo json_encode(array(
                        "success" => true,
                        "fileName" => $fileName,
                        "fileSize" => $fileSize,
                        "msg" => $r->msg.' Upload Success Filename : ' . $fileName . ' Filesize : ' . $fileSize
                    ));
                }
            }
        } else {
//            die("{'success': false, 'error': 'File too big or invalid!'}");
            echo json_encode(array(
                "success" => false,
                "fileName" => $fileName,
                "fileSize" => $fileSize,
                "msg" => 'File too big or invalid!'
            ));
        }
    }
    public function testlogo(){
        $data['nama']='test';
        $r='{"success":false,"msg":"Execute Aborted"}';
        echo $r;
        $arr=json_decode($r);
        echo $arr->msg;
        $r=json_encode(array(
                "success" => 'false',
                "fileName" => 'a',
                "fileSize" => 'b',
                "msg" => 'File too big or invalid!'
            ));
        echo $r;
        echo var_dump(json_decode($r));
                
    }

}

?>
